# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=boost

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.55.0
_boostver=${pkgver//./_}
pkgrel=7
pkgdesc="Free peer-reviewed portable C++ source libraries (mingw-w64)"
arch=('any')
url="http://www.boost.org/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-zlib" "${MINGW_PACKAGE_PREFIX}-bzip2" "${MINGW_PACKAGE_PREFIX}-icu")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-bzip2" "${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3")
options=('strip' 'buildflags' 'staticlibs')
source=(http://downloads.sourceforge.net/sourceforge/boost/boost_${_boostver}.tar.bz2
        boost-1.48.0-add-bjam-man-page.patch
        boost-1.50.0-fix-non-utf8-files.patch
        boost-1.50.0-pool.patch
        boost-1.54.0-bind-static_assert.patch
        boost-1.54.0-bootstrap.patch
        boost-1.54.0-concept-unused_typedef.patch
        boost-1.55.0-log_fix_dump_avx2.patch
        boost-mingw.patch
        using-mingw-w64-python.patch)
md5sums=('d6eef4b4cacb2183f2bf265a5a03a354'
         'dff8166486150121bf89d7050c7e5c49'
         'ef427ef3a47f8fdeeb6b4ae3246aa84f'
         'a69f826fd911dcf34189aaaacf16853f'
         '8b6978fd4ea68bc80b59a96186c8dd4c'
         '7a370bc224e59340b723e775d77b22bf'
         '0ccf6fc4f24afa513063868c51bddd7e'
         'acbc980cd1e8c7dfdbaa380abf95bdc4'
         'c003363c91f6f16294a77c5562c80dbd'
         '96297e17f6babfa41c076e5b47c77c02')

prepare() {
  cd "${srcdir}/boost_${_boostver}"

  patch -p1 -i ${srcdir}/boost-1.50.0-fix-non-utf8-files.patch
  patch -p1 -i ${srcdir}/boost-1.48.0-add-bjam-man-page.patch
  patch -p1 -i ${srcdir}/boost-1.50.0-pool.patch
  patch -p1 -i ${srcdir}/boost-1.54.0-bind-static_assert.patch
  patch -p1 -i ${srcdir}/boost-1.54.0-concept-unused_typedef.patch
  patch -p0 -i ${srcdir}/boost-1.55.0-log_fix_dump_avx2.patch
  patch -p1 -i ${srcdir}/boost-mingw.patch
  patch -p1 -i ${srcdir}/boost-1.54.0-bootstrap.patch
  
  # Use out own build Python
  patch -p1 -i ${srcdir}/using-mingw-w64-python.patch

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null
  
  echo "using python : 2.7 : ${PREFIX_DEPS}/bin/python2.exe : ${PREFIX_DEPS}/include/python2.7 ;" >> ./tools/build/v2/user-config.jam
  echo "using python : 3.4 : ${PREFIX_DEPS}/bin/python3.exe : ${PREFIX_DEPS}/include/python3.4m : ${PREFIX_DEPS}/lib/python3.4/config-3.4m ;" >> ./tools/build/v2/user-config.jam

  mkdir -p "${srcdir}/boost-${MINGW_CHOST}"
  lndir "${srcdir}/boost_${_boostver}" "${srcdir}/boost-${MINGW_CHOST}"
}

build() {
  cd "${srcdir}/boost-${MINGW_CHOST}"
  ./bootstrap.sh --with-icu=${MINGW_PREFIX} --with-python=${MINGW_PREFIX}

  case ${CARCH} in
    i686)
      local _model=32
    ;;
    x86_64)
      local _model=64
    ;;
  esac

  ./b2 \
    toolset=gcc \
    variant=release \
    threading=multi \
    threadapi=win32 \
    link=shared,static \
    runtime-link=shared \
    --debug-configuration \
    --prefix=${pkgdir}${MINGW_PREFIX} \
    -sHAVE_ICU=1 \
    -sICU_PATH=${MINGW_PREFIX} \
    -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
    -sICONV_PATH=${MINGW_PREFIX} \
    -sICONV_LINK="-L${MINGW_PREFIX}/lib -liconv" \
    -sNO_BZIP2 \
    -sBZIP2_BINARY=bz2 \
    -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
    -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
    -sNO_ZLIB \
    -sZLIB_BINARY=z \
    -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
    -sZLIB_LIBPATH=${MINGW_PREFIX}/zlib \
    pch=off \
    -d2 \
    address-model=${_model} \
    --layout=tagged \
    --without-mpi
}

package() {
  cd "${srcdir}/boost-${MINGW_CHOST}"
  case ${CARCH} in
    i686)
      local _model=32
    ;;
    x86_64)
      local _model=64
    ;;
  esac
    ./b2 \
      toolset=gcc \
      variant=release \
      threading=multi \
      threadapi=win32 \
      link=shared,static \
      runtime-link=shared \
      --prefix=${pkgdir}${MINGW_PREFIX} \
      -sHAVE_ICU=1 \
      -sICU_PATH=${MINGW_PREFIX} \
      -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
      -sICONV_PATH=${MINGW_PREFIX} \
      -sICONV_LINK="-L${MINGW_PREFIX}/lib -liconv" \
      -sNO_BZIP2 \
      -sBZIP2_BINARY=bz2 \
      -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
      -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
      -sNO_ZLIB \
      -sZLIB_BINARY=z \
      -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
      -sZLIB_LIBPATH=${MINGW_PREFIX}/zlib \
      address-model=${_model} \
      --layout=tagged install \
      --without-mpi

    mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
    mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin
}
